package com.lvyq.myplayer.adapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.lvyq.myplayer.common.util.DataOperationUtils
import com.lvyq.myplayer.parcelable.SongInfoData
import com.lvyq.myplayer.util.ThreadUtil
import com.lvyq.myplayer.widget.common.song.SongItemOperationView
import org.jetbrains.anko.toast

/**
 * 文件描述：IndexDailyPlayListAdapter  每日推荐
 * 作者：lvyq
 * 创建时间：2025-03-28
 * 修改时间：2025-03-28
 */
class IndexDailyPlayListAdapter(var context:Context ,var datas:List<SongInfoData>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {


    // 定义点击事件接口
    interface OnItemClickListener {
        fun onItemClick(data: SongInfoData, position: Int)
    }

    private var onItemClickListener: OnItemClickListener? = null

    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.onItemClickListener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

        return dailyPlayViewHolder(SongItemOperationView(parent.context))
    }

    override fun getItemCount(): Int {
        return  datas.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
       var data= datas[position]
       var item= holder.itemView as SongItemOperationView
       item.setData(data)
        //添加点击事件
       item.setOnClickListener{
            onItemClickListener?.onItemClick(data,position)
       }
        //添加到下一首
        item.onAddPlayListClickListener={data->
            DataOperationUtils.addWithPositionAndReSet(context,data)
            ThreadUtil.runOnMainThread{
                context.toast("添加到下一首")
            }
        }

      //收藏点击事件
       /* item.onCollectClickListener = { data, isNowCollected ->
            // 处理收藏状态变化
            if (isNowCollected) {
                // 添加到收藏
                MyCollectSharePrefsManager.saveCollectData(context, data)
                context.runOnUiThread { Toast.makeText(this, "收藏成功", Toast.LENGTH_SHORT).show() }
            } else {
                // 从收藏移除
                MyCollectSharePrefsManager.deleteCollectDataById(context, data.id)
                context.runOnUiThread { Toast.makeText(this, "取消收藏", Toast.LENGTH_SHORT).show() }
            }
            // 更新UI
            // 重新设置收藏状态
            item.setData(data)
        }*/

    }

    inner class dailyPlayViewHolder(itemView: View): RecyclerView.ViewHolder(itemView)


}